<template>
  <div class="signIn">
    <el-form
      class="signBox"
      label-width="auto"
      :rules="loginFormRules"
      ref="loginForm"
      :model="loginForm"
      show-message
    >
      <el-form-item prop="userName" class="aMargin">
        <el-input
          type="text"
          prefix-icon="el-icon-user"
          placeholder="请输入账号或邮箱"
          v-model="loginForm.userName"
        ></el-input>
      </el-form-item>
      <el-form-item prop="passWord" class="aMargin">
        <el-input
          type="password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
          v-model="loginForm.passWord"
        ></el-input>
      </el-form-item>
      <el-form-item class="aMargin l">
        <el-button type="primary" class="btn" @click="loginSubmit('loginForm')">登录</el-button>

        <span class="NoUser" @click="goReg">没有账号，去注册>></span>
        <!-- <el-button type="primary" @click="onRegit('loginForm')">注册</el-button> -->
      </el-form-item>
    </el-form>
    <div class="loginTips">绿图科技有限公司版权所有</div>
  </div>
</template>
<script>
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        userName: "",
        passWord: "",
      },
      // 表单验证，需要在 el-form-item 元素中增加 prop 属性
      loginFormRules: {
        userName: [
          { required: true, message: "账号不可为空", trigger: "blur" },
        ],
        passWord: [
          { required: true, message: "密码不可为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    goReg() {
      this.$router.push("/register");
    },
    loginSubmit(formName) {
      // 为表单绑定验证功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 使用 vue-router 路由到指定页面，该方式称之为编程式导航
          // console.log("to loginSuccess");
          // this.$router.replace("/home");
          // console.log(this.loginForm);
          this.$axios.post("/org/client/login", this.loginForm).then((res) => {
            let rr = res.data.data; // 三天有效
            // console.log(rr);
            if (rr) {
              delete rr.passWord
              delete rr.phone
              delete rr.eMail
              delete rr.idCard
              delete rr.contacts
              delete rr.bizLicense
              this.$cookies.set(
                "user_session",
                this.$Base64.encode(JSON.stringify(rr)),
                60 * 60 * 24 * 3
              );
              this.$router.replace("/home");
            }
          });
        } else {
          return false;
        }
      });
    },
  },
  created() {},
};
</script>
<style scoped>
.signIn {
  width: 100%;
  height: 100%;
  background-color: #f2f2f2;
  position: absolute;
  overflow: hidden;
}
.signBox {
  padding: 25px;
  border-radius: 5px;
  box-shadow: 0 0 25px #909399;
  min-width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding-bottom: 10px;
}
.aMargin {
  margin-bottom: 22px;
}
.aMargin.l {
  margin-bottom: 0;
}
.btn {
  width: 100%;
}
.NoUser {
  cursor: pointer;
  float: right;
  color: #888;
}
.loginTips {
  position: absolute;
  bottom: 10px;
  text-align: center;
  width: 100%;
}
</style>
